package com.isunland.managesystem.utils;

import android.widget.TextView;
import com.google.gson.Gson;
import java.io.Serializable;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/3/24 9:47
 */

public class ParamsNotEmpty implements Serializable {
  private HashMap<String, String> params;

  public ParamsNotEmpty() {
    params = new HashMap<>();
  }

  public ParamsNotEmpty put(String key, String value) {
    if (key == null) key = "";
    if (value == null) value = "";
    //防止发生因为有个空格导致参数错误的问题,错误示范如params.put("key "," value ");
    key = key.trim();
    value = value.trim();
    params.put(key, value);
    return this;
  }

  public ParamsNotEmpty put(String key, TextView view) {
    String value = "";
    if (key == null) key = "";
    if (view == null) {
      params.put(key, value);
      return this;
    }
    //防止发生因为有个空格导致参数错误的问题,错误示范如params.put("key "," value ");
    key = key.trim();
    value = view.getText().toString().trim();
    params.put(key, value);
    return this;
  }

  @SuppressWarnings("all") public void putAll(HashMap<String, String> map) {
    if (map == null) return;
    getParams().putAll(map);
  }

  public ParamsNotEmpty putAll(Object obj) {
    if (obj == null) return this;
    getParams().putAll(MyUtils.JsontoHashMap(new Gson().toJson(obj)));
    return this;
  }

  public ParamsNotEmpty putAll(Object obj, TextView... views) {
    putAll(MyUtils.JsontoHashMap(new Gson().toJson(obj)));
    putAll(MyViewUtil.getParams(views));
    return this;
  }

  public HashMap<String, String> getParams() {
    return params == null ? new HashMap<String, String>() : params;
  }
}
